“”“
配置类负责加载和管理 Redhat 虚拟机的配置。
”“”
配置类头部文件
“”“
#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#include <string>
#include <map>
#include <stdexcept>

namespace ebackup {
namespace plugins {

class Configuration {
public:
    Configuration(const std::string& configFilePath);
    ~Configuration();

    std::string getApiUrl() const;
    std::string getUsername() const;
    std::string getPassword() const;
    int getRetentionDays() const;
    bool isCompressionEnabled() const;

private:
    std::string configFilePath_;
    std::map<std::string, std::string> configValues_;

    void loadConfiguration();
    void validateConfiguration();
};

} // namespace plugins
} // namespace ebackup

#endif // CONFIGURATION_H
”“”
配置类实现文件
“”“
#include "Configuration.h"
#include <iostream>
#include <fstream>
#include <map>
#include <stdexcept>

namespace ebackup {
namespace plugins {

Configuration::Configuration(const std::string& configFilePath)
    : configFilePath_(configFilePath) {
    loadConfiguration();
    validateConfiguration();
}

Configuration::~Configuration() {
    // Destructor
}

std::string Configuration::getApiUrl() const {
    return configValues_["api_url"];
}

std::string Configuration::getUsername() const {
    return configValues_["username"];
}

std::string Configuration::getPassword() const {
    return configValues_["password"];
}

int Configuration::getRetentionDays() const {
    return std::stoi(configValues_["retention_days"]);
}

bool Configuration::isCompressionEnabled() const {
    return configValues_["compression_enabled"] == "true";
}

void Configuration::loadConfiguration() {
    std::ifstream configFile(configFilePath_);
    if (!configFile.is_open()) {
        throw std::runtime_error("Failed to open configuration file: " + configFilePath_);
    }

    std::string line;
    while (std::getline(configFile, line)) {
        if (line.find("=") != std::string::npos) {
            size_t pos = line.find("=");
            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos + 1);
            configValues_[key] = value;
        }
    }

    configFile.close();
}

void Configuration::validateConfiguration() {
    if (configValues_.find("api_url") == configValues_.end()) {
        throw std::runtime_error("Missing 'api_url' in configuration file");
    }
    if (configValues_.find("username") == configValues_.end()) {
        throw std::runtime_error("Missing 'username' in configuration file");
    }
    if (configValues_.find("password") == configValues_.end()) {
        throw std::runtime_error("Missing 'password' in configuration file");
    }
    if (configValues_.find("retention_days") == configValues_.end()) {
        throw std::runtime_error("Missing 'retention_days' in configuration file");
    }
    if (configValues_.find("compression_enabled") == configValues_.end()) {
        configValues_["compression_enabled"] = "true"; // Default to true
    }
}

} // namespace plugins
} // namespace ebackup